<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        #btn{
            width: 40px;
            height: 40px;
        }
        html,body{
            width: 100%;
            height: 3000px;
        }
        #panel{
            width: 100%;
            height: 100%;
            opacity: 0.3;
            filter: alpha(opacity:30);
            background: #000;
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }
        #login{
            width: 500px;
            height: 400px;
            margin: 100px auto;
            background: skyblue;
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%);
            display: none;

        }
    </style>
</head>
<body>
<button id="btn">登陆</button>
<div id="panel"></div>
<div id="login"></div>
<script>
    //1.找对象
    var btn = document.getElementById('btn');
    var panel = document.getElementById('panel');
    var login = document.getElementById('login');
    //2.给btn添加我们的点击事件
    btn.onclick = function (event) {
      //2.1.让panel显示出来
        panel.style.display = 'block';
        //2.2.让login也显示出来
        login.style.display = 'block';

        //2.3.接受我们的event事件
        var myEvent = event||window.event;
        //2.4判断是否是火狐/谷歌以及ie
        if(myEvent&&myEvent.stopPropagation)
        {
           myEvent.stopPropagation();
        }
        else
        {
            myEvent.cancelBubble = true;
        }
    };
    //3.当点击了document文档对象的时候,让我们的login隐藏起来(但是除了点击login以外)
    document.onmousedown = function (event) {
        //2.3.接受我们的event事件
        var myEvent = event||window.event;

        //2.4.取出当前触发点击的事件源
        var target = myEvent.target?myEvent.target:myEvent.srcElement;
        //2.5.取出当前触发点击的事件源的id
        var targetId = target.id;
        //2.6.判断是否是点击了login
        if(targetId!='login')
        {
            //2.1.让panel显示出来
            panel.style.display = 'none';
            //2.2.让login也显示出来
            login.style.display = 'none';
        }
    }
</script>
</body>
</html>